<?php //Classe du formulaire.

require_once($CFG->libdir.'/formslib.php');
require_once('lib/mylib.php');

class connexion_tracking_form extends moodleform {

	var $previousCategorie = 0;
	
	function definition() {
	
	global $CFG;
	
	$categoryid         = optional_param('category', null, PARAM_INT);
	
	$mform =& $this->_form;
	//Style inline parce que c'est beaucoup plus simple comme �a et que c'est vraiment trop moche sans.
	$mform->updateAttributes(array( 'style' => "margin-bottom:0px; margin-left:0px; margin-right:20px; margin-top:0px;" ));
		
		//On liste les cat�gories en commen�ant par celles les plus proches de la racine (parent = 0)
		$categories = get_records_select("course_categories", "", "parent ASC");
		$categoriesArray = array();
		$categoriesArray[0] = get_string('allcat', 'block_connexion_tracking');

		foreach($categories as $categorie){
			print_sub_cat($categorie, &$categoriesArray);//Fonction r�cursive qui affiche les sous cat�gories + categories.
		}
		
		//Creation du formaulaire interm�diaire
		$mform->registerNoSubmitButton('getstudents');
		$catgroup = array();
		$select = $mform->createElement('select', 'category', get_string('choixcat', 'block_connexion_tracking'), $categoriesArray);
		//$select->updateAttributes(array( 'onChange' => "getElementById('id_getstudents').click()" ));//Si la liste change, on met � jour l'affichage directement.
		$select->updateAttributes(array( 'onChange' => "asyncRequest();"));
		$catgroup[] =& $select;
		//$catgroup[] =& $mform->createElement('submit', 'getstudents', get_string('listeetu', 'block_connexion_tracking'));
		$mform->addGroup($catgroup, 'catgroup', get_string('choixcat', 'block_connexion_tracking'), array(' '), false);
		$mform->setType('getstudents', PARAM_NOTAGS);
		
		$categoryid = (isset($categoryid)) ? $categoryid : $previousCategorie;
		
		//Requete de listing des etudiants d'une categorie ou de toutes les categories.
		$etudiants = array();
	
		//On place une check box permettant de filtrer tous les �tudiants pour ne voir que les enseignants
		$etuEnsGrp = array();
		$choixGrp = array(0 => get_string('tousEE', 'block_connexion_tracking'), 1 => get_string('etu', 'block_connexion_tracking'), 2 => get_string('ens', 'block_connexion_tracking'));
		$etuEnsGrp[] = &MoodleQuickForm::createElement('select', 'choixGrp', '', $choixGrp);
		$etuEnsGrp[] = &MoodleQuickForm::createElement('select', 'etudiants', get_string('choixetu', 'block_connexion_tracking'), $etudiants);
		$etuEnsGrp['0']->updateAttributes(array( 'onChange' => "asyncRequest();"));
		$etuEnsGrp['1']->updateAttributes(array( 'style' => "display:none;" ));
		$mform->addGroup($etuEnsGrp, 'portee', get_string('target', 'block_connexion_tracking'), ' ', false);
	
		/*AJAX*/
		//Div o� notre callback va �crire en cas d'�rreur.
		$mform->addElement('html','<div id="conteneur"></div>');
		
		//<Script>
		$codeJS = "<script type=\"text/javascript\">";
		//Variable Callback contient les fonctions de succ�s et d'�chec
		//alert(data[0].getAttribute('att1'));
		$codeJS .= "var callback = 
							{ 
							  success: function(o) {
							  		var selectStudent = YAHOO.util.Dom.get('id_etudiants');	
									var students = o.responseXML.getElementsByTagName('student');
									while (selectStudent.childNodes.length>0) {
 										 selectStudent.removeChild(selectStudent.firstChild);
									}
									for (var i=0; i<students.length; i++){
										var option = document.createElement('option');
										option.setAttribute('value', students[i].getAttribute('id'));
										option.appendChild(document.createTextNode(students[i].getAttribute('firstname')+' '+students[i].getAttribute('lastname')));
										var option = selectStudent.appendChild(option);
									}
									var barre = YAHOO.util.Dom.get('loadingBar');
									YAHOO.util.Dom.setStyle(barre, 'display', 'none'); 
									YAHOO.util.Dom.setStyle( selectStudent, 'display', '');
									
								}, 
							  failure: function(o) {
							  		alert('nok');
									var barre = YAHOO.util.Dom.get('loadingBar');
									YAHOO.util.Dom.setStyle(barre, 'display', 'none'); 
								}, 
							  timeout: 5000, 
							}; ";
		//Fonction qui nous permet de filtrer pour obtenir l'element selectcat
		//$codeJS .= "checkName = function(e){ return ( e.getAttribute(\"name\") == \"selectcat\"); };";
		//Code de la fonction de demande asynchrone
		$codeJS .= "function asyncRequest(){
							var barre = YAHOO.util.Dom.get('loadingBar');
							var selectCat = YAHOO.util.Dom.get('id_category');
							var selectPortee = YAHOO.util.Dom.get('id_choixGrp');
							var connectionObject = YAHOO.util.Connect.asyncRequest ('Get', 'http://localhost:8888/Arpe-deployed/blocks/connexion_tracking/list.php?category='+selectCat.options[selectCat.selectedIndex].value+'&portee='+selectPortee.options[selectPortee.selectedIndex].value, callback);
							YAHOO.util.Dom.setStyle(barre, 'display', ''); 
						}";
		//<|Script>
		$codeJS .= "</script>";
        $mform->addElement('html',$codeJS);

		
		$connecdatestartgrp = array();
		$connecdatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'connecstartdate');
		$connecdatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'connecstartenabled', null, '');
		$mform->addGroup($connecdatestartgrp, 'connecstartdategrp', get_string('ddebut', 'block_connexion_tracking'), ' ', false);
		$mform->setDefault('connecstartdate', 0);
		$mform->setDefault('connecstartenabled', 0);
		$mform->disabledIf('connecstartdategrp', 'connecstartenabled');
		
		$connecdateendgrp = array();
		$connecdateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'connecenddate');
		$connecdateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'connecendenabled', null, '');
		$mform->addGroup($connecdateendgrp, 'connecdateendgrp', get_string('dfin', 'block_connexion_tracking'), ' ', false);
		$mform->setDefault('connecenddate', 0);
		$mform->setDefault('choixGrp', 0);
		$mform->setDefault('etudiants', 0);
		$mform->setDefault('category', 0);//Par d�faut on liste tous les etudiants de toutes les categories.
		$mform->setDefault('connecendenabled', 0);
		$mform->disabledIf('connecdateendgrp', 'connecendenabled');
		$this->add_action_buttons($cancel = true, $submitlabel=get_string('submitbutton', 'block_connexion_tracking'));
		
		//On cr�e aussi une boite pour la barre de progression de la requ�te
		$mform->addElement('html','<div id="loadingBar" style="display:none;text-align: center;position: relative;margin-left: auto; margin-right: auto; width:200px;"><img src="'.$CFG->wwwroot."/blocks/connexion_tracking/img/activity.gif".'" /></div>');
	}
	
	function validation($data) {
	$errors= array();
		global $SESSION;
		if((strcmp($SESSION->connexion_tracking_request->category, $data[category])) && isset($SESSION->connexion_tracking_request->category)){
			$errors['catgroup'] = get_string('catgrouperror', 'block_connexion_tracking');
		}
        if ($data['connecstartenabled'] && $data['connecendenabled']){
            if ($data['connecstartdate'] >= $data['connecenddate']){
                $errors['connecdateendgrp'] = get_string('connecdateerror', 'block_connexion_tracking');
            }
        }
        return $errors;	
	}


}

?>
